<?php

namespace app\controller\admin\cnf;

use core\controller\AdminBase;
use app\bll\ComponentWidget as BllComponentWidget;
use Exception;
use support\View;

/**
 * 挂件控制器
 */
class ComponentWidget extends AdminBase
{

    public function index()
    {
        $bllComponentWidget = new BllComponentWidget();
        $widgets = $bllComponentWidget->paginator(
            ident: request()->get('ident', ''),
            title: request()->get('title', '')
        );
        View::assign('widgets', $widgets);
        return $this->view();
    }

    public function install()
    {
        if (is_post()) {
            try {
                $bllComponentWidget = new BllComponentWidget();
                $bllComponentWidget->install(request()->input('type'));
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view(frame: false);
        }
    }

    public function onlineUpgrade()
    {
        try {
            $bllComponentWidget = new BllComponentWidget();
            $bllComponentWidget->onlineUpgrade(
                request()->input('id')
            );
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }


    /**
     * 升级挂件
     *
     * @return void
     */
    public function handleUpgrade()
    {
        if (is_post()) {
            try {
                $bllComponentWidget = new BllComponentWidget();
                $bllComponentWidget->handleUpgrade(
                    request()->input('id')
                );
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view(frame: false);
        }
    }

    /**
     * 卸载挂件
     *
     * @return void
     */
    public function  uninstall()
    {
        try {
            $bllComponentWidget = new BllComponentWidget();
            $bllComponentWidget->uninstall(
                request()->input('id')
            );
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
